define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    "index_url": "record/classes/index",
                    "add_url": "record/classes/add",
                    "edit_url": "record/classes/edit",
                    "del_url": "record/classes/del",
                    "multi_url": "record/classes/multi",
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                commonSearch: true,
                searchFormVisible: true,
                sortName: 'id',
                sortOrder: 'asc',
                columns: [
                    [
                        {field: 'state', checkbox: true,},
                        {field: 'json_data.school_name', title: __('学校'), operate: false},
                        {field: 'json_data.grade_name', title: __('年级'), operate: false},
                        {field: 'json_data.class_name', title: __('班级'), operate: false},
                        {field: 'json_data.headmaster_name', title: __('班主任'), operate: false},
                        {field: 'type', title: __('点评类别'), operate: false},
                        {field: 'behavior_category_name', title: __('点评分类'), operate: false},
                        {field: 'record_username', title: __('点评人'), operate: false},
                        {field: 'behavior_name', title: __('点评行为'), operate: false},
                        {
                            field: 'behavior_type_text',
                            title: __('类型'),
                            operate: false,
                            formatter: Controller.api.formatter.label
                        },
                        {field: 'score', title: __('点评分值'), operate: false},
                        {
                            field: 'created_at',
                            title: __('Create time'),
                            sortable: true,
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange'
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $(document).on('click', '.btn-import', function () {
                Backend.api.open('record/classes/import', '导入');
            });
        },
        add: function () {
            Controller.api.bindevent($("form[role=form]"));
        },
        edit: function () {
            Controller.api.bindevent($("form[role=form]"));
        },
        import: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(document).on('click', '.behavior_image', function () {
                    var image_id = $(this).data('id');
                    $('.behavior_container').removeClass('selected');
                    $('.behavior_container_' + image_id).addClass('selected');

                    $("#image_id").val(image_id);
                });
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                label: function (value, row, index) {
                    if (row.behavior_type == '1') {
                        return '<span class="label label-success">' + value + '</span>';
                    } else {
                        return '<span class="label label-danger">' + value + '</span>';
                    }
                }
            },
        }
    };
    return Controller;
});
